<template>
  <div class="search-form">
    <!-- 头部tab切换 -->
    <el-row type="flex" class="search-tab">
      <span
        v-for="(item, index) in tabs"
        :key="index"
        @click="handleSearchTab(item, index)"
        :class="{active: index === currentTab}"
      >
        <i :class="item.icon"></i>
        {{item.name}}
      </span>
    </el-row>

    <el-form class="search-form-content" ref="form" label-width="80px">
      <el-form-item label="出发城市">
        <!-- fetch-suggestions 返回输入建议的方法 -->
        <!-- select 点击选中建议项时触发 -->
        <!-- highlight-first-item   是否默认突出显示远程搜索建议中的第一项  -->
        <!-- trigger-on-focus 是否在输入框 focus 时显示建议列表 -->
        <el-autocomplete
          :highlight-first-item="true"
          :trigger-on-focus="false"
          :fetch-suggestions="getSearch"
          placeholder="请搜索出发城市"
          @select="handleDepartSelect"
          @blur="blurDepart"
          class="el-autocomplete"
          v-model="form.departCity"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="到达城市">
        <el-autocomplete
          :highlight-first-item="true"
          :trigger-on-focus="false"
          :fetch-suggestions="getSearch"
          placeholder="请搜索到达城市"
          @select="handleDestSelect"
          @blur="blurDest"
          class="el-autocomplete"
          v-model="form.destCity"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="出发时间">
        <!-- change 用户确认选择日期时触发 -->
        <el-date-picker
          type="date"
          placeholder="请选择日期"
          style="width: 100%;"
          @change="handleDate"
          v-model="form.departDate"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label>
        <el-button style="width:100%;" type="primary" icon="el-icon-search" @click="handleSubmit">搜索</el-button>
      </el-form-item>
      <div class="reverse">
        <span @click="handleReverse">换</span>
      </div>
    </el-form>
  </div>
</template>

<script>
// npm install --save moment  导入 日期处理类库 http://momentjs.cn/
import moment from "moment";
export default {
  data() {
    return {
      tabs: [
        { icon: "iconfont icondancheng", name: "单程" },
        { icon: "iconfont iconshuangxiang", name: "往返" }
      ],
      currentTab: 0,
      form: {
        departCity: "",
        dsepartCode: "",
        destCity: "",
        destCode: "",
        departDate: ""
      },
      inputBlurData: {}
    };
  },
  methods: {
    // tab切换时触发
    handleSearchTab(item, index) {
      if ((index = 1)) {
        this.$notify({
          title: "警告",
          message: "往返功能占未开通",
          type: "warning"
        });
      }
    },

    // 出发城市输入框获得焦点时触发
    // value 是选中的值，showList是回调函数，接收要展示的列表
    getSearch(value, showList) {
      if (!value) {
        return;
      }
      this.$axios({
        method: "get",
        url: "/airs/city",
        params: { name: value }
      }).then(res => {
        let { data } = res.data;
        if (data.length > 0) {
          // 正则表达式 判断是否最后一位有市字
          data.forEach((element, index) => {
            // 字符串方法替换掉市字
            // data[index].name = element.name.replace("市", "");
            // 正则方法替换掉市字
            data[index].name = element.name.replace(/市$/, "");
          });
          // 过滤掉没有 城市标记代码的城市
          data = data.filter(item => {
            // 若有的城市的城市代码sort 为null  就不返回
            return item.sort;
          });
          showList(
            data.map(item => {
              return {
                ...item,
                value: item.name
              };
            })
          );
          // 给失去焦点的 值 复制 默认第一个
          this.inputBlurData = data[0];
        } else {
          showList([{ value: "很抱歉,搜索不出来" }]);
        }
      });
      // showList([
      //     {value: 1},
      //     {value: 2},
      //     {value: 3},
      // ]);
    },
    // 出发城市下拉选择时触发
    handleDepartSelect(item) {
      // console.log("下拉选择");
      this.form.departCity = item.name;
      this.form.dsepartCode = item.sort;
      // 清除失去焦点需要的数据
      // console.log(this.form.departCity);
      
      this.inputBlurData = {};
      // console.log("清除数据成功");
      // console.log(" this.inputBlurData: ", this.inputBlurData.name);
    },
    // 出发城市失去焦点时出发
    blurDepart() {
      // 延时器 失去焦点触发比选择器快
      setTimeout(() => {
        // console.log("失去焦点");
        // console.log(" this.inputBlurData: ", this.inputBlurData.name);
        // 失去焦点数据
        if (this.inputBlurData.name) {
          //  console.log(this.form.departCity);
          this.form.departCity = this.inputBlurData.name;
          this.form.dsepartCode = this.inputBlurData.sort;
        }
        // 清除失去焦点需要的数据
        this.inputBlurData = {};
      }, 200);
    },

    // 目标城市下拉选择时触发
    handleDestSelect(item) {
      this.form.destCity = item.name;
      this.form.destCode = item.sort;
      this.inputBlurData = {};
    },
    blurDest() {
      setTimeout(() => {
        if (this.inputBlurData.name) {
          this.form.destCity = this.inputBlurData.name;
          this.form.destCode = this.inputBlurData.sort;
        }
        this.inputBlurData = {};
      }, 200);
    },

    // 确认选择日期时触发
    handleDate(value) {
      // this.form.Date = moment(value).format("YYYY-MM-DD");
    },

    // 触发和目标城市切换时触发
    handleReverse() {
      const exchange = this.form.departCity;
      const exchangeCode = this.form.dsepartCode;
      this.form.departCity = this.form.destCity;
      this.form.dsepartCode = this.form.destCode;
      this.form.destCity = exchange;
      this.form.destCode = exchangeCode;
    },

    // 提交表单是触发
    handleSubmit() {

let full = true;
for (const key in this.form) {
  if (this.form.hasOwnProperty(key)) {
    const element = this.form[key];
    
    if (!element) {
      full = false
      break
    }
  }
}
// 是否所有属性到期
if (full) {
  this.$store.commit('history/lsitPush',this.form)
 this.$message({
          message: '正在跳转 请稍等',
          type: 'success'
        });

 this.$router.push({
    path: '/air/flights',
    // 接下来需要拼接数据,问好数据就是 query 传递
    query: this.form
  })
}else{
    this.$message.error('对不起 您输入的参数有误');
}

      
    }
  },
  mounted() {}
};
</script>

<style scoped lang="less">
.search-form {
  border: 1px #ddd solid;
  border-top: none;
  width: 360px;
  height: 350px;
  box-sizing: border-box;
}

.search-tab {
  span {
    display: block;
    flex: 1;
    text-align: center;
    height: 48px;
    line-height: 42px;
    box-sizing: border-box;
    border-top: 3px #eee solid;
    background: #eee;
  }

  .active {
    border-top-color: orange;
    background: #fff;
  }

  i {
    margin-right: 5px;
    font-size: 18px;

    &:first-child {
      font-size: 16px;
    }
  }
}

.search-form-content {
  padding: 15px 50px 15px 15px;
  position: relative;

  .el-autocomplete {
    width: 100%;
  }
}

.reverse {
  position: absolute;
  top: 35px;
  right: 15px;

  &:after,
  &:before {
    display: block;
    content: "";
    position: absolute;
    left: -35px;
    width: 25px;
    height: 1px;
    background: #ccc;
  }

  &:after {
    top: 0;
  }

  &:before {
    top: 60px;
  }

  span {
    display: block;
    position: absolute;
    top: 20px;
    right: 0;
    font-size: 12px;
    background: #999;
    color: #fff;
    width: 20px;
    height: 20px;
    line-height: 18px;
    text-align: center;
    border-radius: 2px;
    cursor: pointer;

    &:after,
    &:before {
      display: block;
      content: "";
      position: absolute;
      left: 10px;
      width: 1px;
      height: 20px;
      background: #ccc;
    }

    &:after {
      top: -20px;
    }

    &:before {
      top: 20px;
    }
  }
}
</style>
